package state.component;

import particle.core.view.controls.Container;
import particle.core.view.controls.EditBox;
import state.GameConstants;
import state.GameGlobal;
import state.Graphics;
import state.Manager;
import state.Sprite;
import state.font.ImageText;

public class TextField extends Component {
  public static final int DEFAULT_KEY_DELAY = 1000;

  public static final int CONSTRAINT_ANY = 0;
  public static final int CONSTRAINT_PASSWORD = 1;
  public static final int CONSTRAINT_PHONE_NUMBER = 3;// allow input phone number
  public static final int CONSTRAINT_NUMERIC = 4; // allow input integer number
  public static final int CONSTRAINT_USER_NAME = CONSTRAINT_ANY;
// public static final int CONSTRAINT_URL = 6; // allow input url
  public static final int CONSTRAINT_EMAIL_ADDRESS = CONSTRAINT_ANY;

  private int constraint = CONSTRAINT_ANY;
  private EditBox systemTextField = null;

  public TextField(Manager manager, int constraint1, TextFieldStyle style) {
    super(manager, 0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
    initComponentForTextField();
    this.constraint = constraint1;
    if (style != null) {
      setStyleOfTextField(style);
    }
    if (manager != null) {
      setManager(manager);
    }
  }

  public void initComponentForTextField() {
    systemTextField = new EditBox();
    Container.createAndAddControl(systemTextField, GameGlobal.systemCanvas.getContainer(), GameGlobal.systemCanvas.getControlData());
    systemTextField.setVisible(true);
    systemTextField.setEnabled(true);
    super.initComponent();
    switch (constraint) {
    case CONSTRAINT_PASSWORD:
      systemTextField.setPassword();
      break;
    case CONSTRAINT_NUMERIC:
    case CONSTRAINT_PHONE_NUMBER:
      systemTextField.setNumeric();
      break;
    }
  }

  public TextField setStyleOfTextField(TextFieldStyle style) {
    setForceGroundColor(style.forceGroundColor);
    setBackgroundColor(style.backgroundColor);
    setEnable(style.enable);
    setHasBorder(style.hasBorder);
    setBorderColor(style.borderColor);
    setFocused(style.focused);
    setFocusable(style.focusable);
    setFillBackGround(style.fillBackGround);
    setFocusBgColor(style.focusBgColor);
    setFocusFgColor(style.focusFgColor);
    setBorderColor(style.borderColor);
    setFont(style.font);
    setAlign(style.align);
    return this;
  }

  public int getFocusBgColor() {
    return systemTextField.getBgColor();
  }

  public int getFocusFgColor() {
    return systemTextField.getTextColor();
  }

  public int getBackgroundColor() {
    return systemTextField.getBgColor();
  }

  public Component setBackgroundColor(int backgroundColor) {
    systemTextField.setBgColor(backgroundColor);
    return this;
  }

  public boolean isFillBackGround() {
    return true;
  }

  public int getForceGroundColor() {
    return systemTextField.getTextColor();
  }

  public Component setForceGroundColor(int forceGroundColor) {
    systemTextField.setTextColor(forceGroundColor);
    return this;
  }

  public boolean isEnable() {
    return systemTextField.isEnabled();
  }

  public Component setEnable(boolean isEnable) {
    systemTextField.setEnabled(isEnable);
    return this;
  }

  public boolean isHasBorder() {
    return systemTextField.getBorderWidth() == 0;
  }

  public Component setHasBorder(boolean hasBorder) {
    systemTextField.setBorderWidth(hasBorder ? 1 : 0);
    return this;
  }

  public int getBorderColor() {
    return systemTextField.getBorderColor();
  }

  public Component setBorderColor(int borderColor1) {
    systemTextField.setBorderColor(borderColor1);
    return this;
  }

  public boolean isFocused() {
    return systemTextField.isFocused();
  }

  public Component setFocused(boolean isFocused) {
    systemTextField.setFocusable(true);
    systemTextField.setFocused(isFocused);
    return this;
  }

  public boolean isFocusable() {
    return systemTextField.isFocusable();
  }

  public Component setFocusable(boolean focusable) {
    systemTextField.setFocusable(focusable);
    return this;
  }

  public void keyPressed(Sprite source, int keyCode) {
  }

  public void keyReleased(Sprite source, int keyCode) {
  }

  public void keyRepeated(Sprite source, int keyCode) {
  }

  public void pointerDragged(Sprite source, int x1, int y1) {
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
  }

  public Sprite setAnchor(int anchor1) {
    this._anchor = anchor1;
    int x1 = Sprite.calculateX(getX(), getAnchor(), getWidth());
    int y1 = Sprite.calculateY(getY(), getAnchor(), getHeight());
    systemTextField.setPosition(x1, y1);
    return this;
  }

  public int getAnchor() {
    return this._anchor;
  }

  public int getHeight() {
    return systemTextField.getHeight();
  }

  public int getWidth() {
    return systemTextField.getWidth();
  }

  public int getRealX() {
    return systemTextField.getX();
  }

  public int getRealY() {
    return systemTextField.getY();
  }

  public int getX() {
    return _x;
  }

  public int getY() {
    return _y;
  }

  public boolean isVisible() {
    return systemTextField.isVisible();
  }

  public Sprite setVisible(boolean isVisible) {
    systemTextField.setVisible(isVisible);
    return this;
  }

  public void draw(Graphics g) {
  }

  public Component setFont(ImageText font) {
    super.setFont(font);
    systemTextField.setFont(font.getSystemFont());
    return this;
  }

  public int getConstraint() {
    return constraint;
  }

  public TextField setConstraint(int constraint1) {
    this.constraint = constraint1;
    return this;
  }

  public int getAlign() {
    return systemTextField.getAlignment();
  }

  public TextField setAlign(int align) {
    systemTextField.setAlignment(align);
    return this;
  }

  public Sprite setHeight(int height) {
    systemTextField.setHeight(height);
    return this;
  }

  public Sprite setPosition(int x2, int y2) {
    this._x = x2;
    this._y = y2;
    int x1 = Sprite.calculateX(getX(), getAnchor(), getWidth());
    int y1 = Sprite.calculateY(getY(), getAnchor(), getHeight());
    systemTextField.setPosition(x1, y1);
    return this;
  }

  public Sprite move(int dx, int dy) {
    if ((dx == 0) && (dy == 0)) {
      return this;
    }

    _x += dx;
    _y += dy;
    int x1 = calculateX(getX(), getAnchor(), getWidth());
    int y1 = calculateY(getY(), getAnchor(), getHeight());
    systemTextField.setPosition(x1, y1);
    return this;
  }

  public Sprite setSize(int width, int height) {
    if (systemTextField != null) {
      systemTextField.setSize(width, height);
    }
    return this;
  }

  public Sprite setWidth(int width) {
    systemTextField.setWidth(width);
    return this;
  }

  public int getMaxLength() {
    return systemTextField.getMaxChars();
  }

  public TextField setMaxLength(int maxLength) {
    systemTextField.setMaxChars(maxLength);
    return this;
  }

  public String getText() {
    return systemTextField.getText();
  }

  public TextField setText(String text) {
    systemTextField.setText(text);
    return this;
  }

  public TextField append(String text) {
    systemTextField.setText(systemTextField.getText() + text);
    return this;
  }

  public TextField appendChar(char text) {
    systemTextField.setText(systemTextField.getText() + text);
    return this;
  }

  public TextField clearText() {
    systemTextField.setText("");
    return this;
  }

  public TextField deleteLastChar() {
    if (systemTextField.getText().length() > 0) {
      systemTextField.setText(systemTextField.getText().substring(0, systemTextField.getText().length() - 1));
    }
    return this;
  }

  public Sprite detroy() {
    super.detroy();
    systemTextField.destroy();
    return this;
  }

  public void keyReleased1(int keyCode) {
  }
}
